# 导入flask
from flask import Flask,request
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
import json

# 实例化Flask
app = Flask(__name__)

app.config.from_object(Config)

db = SQLAlchemy(app)

CORS(app)
class Order(db.Model):
    __tablename__ = 'order'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(30),unique=True)
    price = db.Column(db.Float,default=0)

# db.create_all()

@app.route('/shop/',methods=['POST','GET','DELETE'])
def shop():
    if request.method == 'POST':
        data = json.loads(request.data)
        name = data['name']
        price = data['price']
        
        shop = Order(name=name,price=price)
        
        try:
            db.session.add(shop)
            db.session.commit()
            return {"code":200}
        except:
            return {"code":10010}
    elif request.method == 'GET':
        shop = Order.query.all()
        list = []
        for i in shop:
            dict1 = {}
            dict1['id']=i.id
            dict1['name']=i.name
            dict1['price']=i.price
            list.append(dict1)
            
        return {"code":200,'list':list}
    else:
        id =request.args.get('id')
        Order.query.filter_by(id=id).delete()
        db.session.commit()
        return {'code':200}
    





if __name__ == '__main__':
    app.run()
    
